package com.iknow.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.User;
import com.iknow.data.SubscribeTag;
import com.iknow.db.IKnowDataBase;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.SubscribeTagAdapter;
import com.iknow.util.Loger;
import com.iknow.view.widget.MyListView;
import com.mobclick.android.MobclickAgent;
import java.util.List;

public class FlagActivity extends Activity
{
  private final String TAG = "FlagActivity";
  private AdapterView.OnItemClickListener listItemClickListener;
  private Button mBackButton;
  protected MyListView mListView;
  private TaskAdapter mListener;
  protected SubscribeTagAdapter mTagAdapter;
  protected List<SubscribeTag> mTagList;
  protected FlagTask mTask;
  protected User mUser;
  protected ProgressBar pb;
  protected Button refresh;

  public FlagActivity()
  {
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        SubscribeTag localSubscribeTag = this.this$0.mTagAdapter.getItem(paramInt);
        List localList = localSubscribeTag.getChildList();
        Intent localIntent = new Intent();
        if ((localList != null) && (localList.size() > 1))
        {
          localIntent.setClass(this.this$0, FlagItemChildActivity.class);
          localIntent.putExtra("child", (String[])localList.toArray(new String[0]));
          localIntent.putExtra("tag", localSubscribeTag.getTagName());
          MobclickAgent.onEvent(this.this$0, "tag_click", localSubscribeTag.getTagName());
        }
        while (true)
        {
          while (true)
          {
            this.this$0.startActivity(localIntent);
            return;
            localIntent.setClass(this.this$0, TagQingboActivity.class);
            if (localList == null)
              break;
            localIntent.putExtra("tag", new SubscribeTag((String)localList.get(0), "1"));
          }
          localIntent.putExtra("tag", localSubscribeTag);
        }
      }
    };
    this.mListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "FlagTask";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (paramTaskResult == TaskResult.OK)
        {
          this.this$0.mTagAdapter.clearTag();
          if (this.this$0.mUser != null)
            this.this$0.mTagAdapter.setMySubscribeTagList(IKnow.mIKnowDataBase.getSubscribeTags(this.this$0.mUser.getUID()));
          this.this$0.mTagAdapter.setTagList(this.this$0.mTagList);
        }
        while (true)
        {
          this.this$0.mTagAdapter.notifyDataSetChanged();
          this.this$0.pb.setVisibility(8);
          this.this$0.refresh.setVisibility(0);
          return;
          Toast.makeText(this.this$0, this.this$0.mTask.getErrorMessage(), 0).show();
        }
      }

      public void onPreExecute()
      {
        if (this.this$0.pb.getVisibility() == 8)
        {
          this.this$0.pb.setVisibility(0);
          this.this$0.refresh.setVisibility(8);
        }
      }
    };
  }

  protected void getSubscribeTag()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mTask = new FlagTask(this, null);
      this.mTask.setListener(this.mListener);
      this.mTask.execute(new TaskParams[0]);
    }
  }

  protected void initView()
  {
    setContentView(2130903128);
    this.mBackButton = ((Button)findViewById(2131296342));
    this.mTagAdapter = new SubscribeTagAdapter(getLayoutInflater());
    this.mListView = ((MyListView)findViewById(2131296535));
    this.pb = ((ProgressBar)findViewById(2131296345));
    this.refresh = ((Button)findViewById(2131296344));
    this.refresh.setText("刷新");
    this.mListView.setAdapter(this.mTagAdapter);
    this.mListView.setOnItemClickListener(this.listItemClickListener);
    this.mBackButton.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    });
    this.refresh.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.pb.setVisibility(8);
        this.this$0.getSubscribeTag();
      }
    });
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    initView();
    getSubscribeTag();
  }

  protected void onDestroy()
  {
    super.onDestroy();
  }

  protected void onPause()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mTask.cancel(true);
    MobclickAgent.onPause(this);
    super.onPause();
  }

  protected void onResume()
  {
    super.onResume();
    this.mUser = IKnow.mIKnowDataBase.getUser();
    if (this.mTagAdapter == null);
    while (true)
    {
      return;
      this.mTagAdapter.clearTag();
      if (this.mUser != null)
        this.mTagAdapter.setMySubscribeTagList(IKnow.mIKnowDataBase.getSubscribeTags(this.mUser.getUID()));
      if (this.mTagList != null)
        this.mTagAdapter.setTagList(this.mTagList);
      this.mTagAdapter.notifyDataSetChanged();
      MobclickAgent.onResume(this);
      MobclickAgent.setSessionContinueMillis(600000L);
    }
  }

  private class FlagTask extends GenericTask
  {
    private String mErrorMessage;

    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      try
      {
        this.this$0.mTagList = IKnow.mApi.getSubscribeTags();
        localTaskResult = TaskResult.OK;
        return localTaskResult;
      }
      catch (Exception localException)
      {
        this.mErrorMessage = "网络超时,稍后再试";
        Loger.e("FlagActivity", this.mErrorMessage);
        localTaskResult = TaskResult.FAILED;
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }
  }
}